﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;

namespace OCRTheNewHope
{
    class Program
    {

        static bool CheckValidity(Point[] points)
        {
            bool isValid = true;
            foreach (Point p in points)
                if (p.X < 0 || p.X > 27 || p.Y < 0 || p.Y > 27)
                    isValid = false;
            
            return isValid;
        }

        static void Main(string[] args)
        {
            //Digit[] db = FileOperations.ReadDatabase("C:\\BIG-heavy.data");

            Random rnd = new Random();

            for (int m = 0; m < 100; m++)
            {
                Bitmap bmp = new Bitmap(28, 28);

                Point A, B, C, D;

                int x, y;

                double heightShift, widthShift;

                int heightShiftInteger, widthShiftInteger;

                while (true)
                {
                    for (int i = 0; i < 28; i++)
                    {
                        for (int j = 0; j < 28; j++)
                        {
                            bmp.SetPixel(i, j, Color.FromArgb(0, 0, 0, 0));
                        }
                    }

                    int width = rnd.Next(28);
                    int height = rnd.Next(28);


                    x = rnd.Next(28);
                    y = rnd.Next(28);

                    heightShift = height / Math.Sqrt(2);
                    widthShift = width / Math.Sqrt(2);

                    heightShiftInteger = (int)Math.Round(heightShift, 0, MidpointRounding.AwayFromZero);

                    widthShiftInteger = (int)Math.Round(widthShift, 0, MidpointRounding.AwayFromZero);

                    D = new Point(x, y);

                    B = new Point(x + heightShiftInteger, y + heightShiftInteger);

                    C = new Point(x - widthShiftInteger, y + widthShiftInteger);

                    A = new Point(x + heightShiftInteger - widthShiftInteger, y + heightShiftInteger + widthShiftInteger);

                    //if (B.X > 0 && B.Y > 0 && C.X > 0 && C.Y > 0 && D.X > 0 && D.Y > 0 && B.X < 27 && B.Y < 27 && C.X < 27 && C.Y < 27 && D.X < 27 && D.Y < 27)
                    if (CheckValidity(new Point[] { A, B, C, D }))
                    {
                        break;
                    }
                }


                bmp.SetPixel(D.X, D.Y, Color.FromArgb(255, 255, 255, 255));

                bmp.SetPixel(B.X, B.Y, Color.FromArgb(255, 255, 255, 255));

                bmp.SetPixel(C.X, C.Y, Color.FromArgb(255, 255, 255, 255));

                bmp.SetPixel(A.X, A.Y, Color.FromArgb(255, 255, 255, 255));

                bmp.Save("C:\\k\\caps"+m.ToString()+".png");
            }

            //Digit[] db = FileOperations.ReadDatabase("C:\\integral_train.data");
            //Digit[] db = FileOperations.ReadDatabaseOld("C:\\test.data");

            //BinaryWriter b = FileOperations.CreateHeader("C:\\integral_test.data", 10000, 28, 28);
            //FileOperations.WriteDoubleIntegralDatabase(b, db);

            //Parameters.GlobalLogPath = FileOperations.GetLogPath();
            //Parameters.GlobalImagePath = FileOperations.GetImagePath();

            //using (StreamWriter writer = File.CreateText(Parameters.GlobalLogPath))
            //{
            //    writer.WriteLine(Parameters.Log());
            //}

            //Forest mainForest = new Forest();

            //using (StreamWriter writer = File.AppendText(Parameters.GlobalLogPath))
            //{
            //    writer.WriteLine();
            //    writer.WriteLine("Finished at: " + DateTime.Now);
            //}
        }        
    } 
}
